//
//  ViewController.m
//  autolayout学习
//
//  Created by song on 15/9/10.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    [self useMasnory];
    

    
    UIView *redview = [[UIView alloc] init];
    redview.backgroundColor =[UIColor redColor];
    redview.translatesAutoresizingMaskIntoConstraints =NO;
     [self.view addSubview:redview];
     UIView *greenview =[[UIView alloc] init];
    [greenview setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:greenview];
    greenview.translatesAutoresizingMaskIntoConstraints =NO; 
    CGFloat margin =20;
    [redview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(margin);
        make.right.equalTo(self.view.mas_right).with.offset(-margin);
        make.top.equalTo(self.view.mas_top).with.offset(margin);
        make.height.mas_equalTo(@40);
    }];
    
    [greenview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).with.offset(-margin);
        make.height.equalTo(redview);
        make.top.equalTo(redview.mas_bottom).with.offset(margin);
        make.width.equalTo(redview.mas_width).multipliedBy(0.5);
    }];
    
}



-(void)useMasnory
{
    
}


-(void)useAutoLayout{
    
    
    UIView *redview = [[UIView alloc] init];
    redview.backgroundColor =[UIColor redColor];
    redview.translatesAutoresizingMaskIntoConstraints =NO;
    [self.view addSubview:redview];
    UIView *greenview =[[UIView alloc] init];
    [greenview setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:greenview];
    greenview.translatesAutoresizingMaskIntoConstraints =NO;
    
    // 设置高度
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:40.0f];
    [redview addConstraint:height];
    CGFloat margin =20;
    // 设置左侧的间距
    NSLayoutConstraint *redleft =[NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:margin];
    [self.view addConstraint:redleft];
    //设置顶端的约束
    NSLayoutConstraint *redtop =[NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:margin];
    // 设置右端的约束
    NSLayoutConstraint *redright =[NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-margin];
    [self.view addConstraint:redtop];
    [self.view addConstraint:redright];
    
    
    NSLayoutConstraint *greenright = [NSLayoutConstraint constraintWithItem:greenview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:redview attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
    
    NSLayoutConstraint *greenheight =[NSLayoutConstraint constraintWithItem:greenview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:redview attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    
    NSLayoutConstraint *greentop = [NSLayoutConstraint constraintWithItem:greenview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:redview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:margin];
    NSLayoutConstraint *greenwith = [NSLayoutConstraint constraintWithItem:greenview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:redview attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
    
    [self.view addConstraint:greenright];
    [self.view addConstraint:greenheight];
    [self.view addConstraint:greentop];
    [self.view addConstraint:greenwith];
    
    
    
    /**  设置redview 居中对齐，宽度高度限定100
     redview.translatesAutoresizingMaskIntoConstraints =NO;
     NSLayoutConstraint *width= [NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100];
     [redview addConstraint:width];
     NSLayoutConstraint *height= [NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeHeight            relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100];
     [redview addConstraint:height];
     NSLayoutConstraint *centerX   = [NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeCenterX                   relatedBy:NSLayoutRelationEqual toItem:self.view  attribute:NSLayoutAttributeCenterX   multiplier:1.0f constant:0.0];
     [self.view addConstraint:centerX];
     NSLayoutConstraint *centerY   = [NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeCenterY                   relatedBy:NSLayoutRelationEqual toItem:self.view  attribute:NSLayoutAttributeCenterY   multiplier:1.0f constant:0.0];
     [self.view addConstraint:centerY];
     **/
    
    
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
